package jp.sourceforge.akjrcp.ui;

import java.io.File;

import jp.sourceforge.akjrcp.image.trimming.DrawImageFigure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

public class SimpleImageCanvas extends FigureCanvas{
private String imagePath;
private DrawImageFigure imageLabel;
private String lastData="";
private Image image;
	public SimpleImageCanvas(Composite parent) {
		super(parent);
		// TODO Auto-generated constructor stub
		imageLabel=new DrawImageFigure();
		
		this.setContents(imageLabel);
	}

	public Figure getRootFigure(){
		return imageLabel;
	}
	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String path){
		if(path==null){
			imageLabel.setImage(null);
			imageLabel.repaint();
			lastData="";
			return;
		}
		File file=new File(path);
		if(!file.exists()){
			imageLabel.setImage(null);
			imageLabel.repaint();
		}else{
			String checkData=path+file.lastModified();
			if(checkData.equals(lastData)){
				//same
				imageLabel.repaint();
				return;
			}else{
				Image old=imageLabel.getImage();
				if(old!=null){
					old.dispose();
				}
				lastData=checkData;
				image=new Image(null,path);
				imageLabel.setImage(image);
				imageLabel.repaint();
				imagePath=path;
			}
		}
	}

}
